/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.sgf.provider.vo.custom;

import com.inspur.edp.formserver.viewmodel.GspViewModel;
import com.inspur.edp.formserver.viewmodel.action.ViewModelAction;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import com.inspur.edp.sgf.api.common.SummaryInfo;
import com.inspur.edp.sgf.provider.vo.basic.SimpleBasicActionSummary;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述: @ClassName: SimpleCustomActionProvider @Author: Fynn Qi @Date: 2020/7/31 10:44 @Version:
 * V1.0
 */
public class SimpleCustomActionSummary {

    public static List<SummaryInfo> getSimpleCustomActions(GspMetadata gspMetadata) {
        GspViewModel viewModel = (GspViewModel) gspMetadata.getContent();
        List<SummaryInfo> operations = new ArrayList<>();
        List<ViewModelAction> actions = viewModel.getActions();
        for (ViewModelAction action : actions) {
            long count =
                    SimpleBasicActionSummary.getSimpleBasicActions(gspMetadata).stream()
                            .filter(x -> x.getCode().equals(action.getCode()))
                            .count();
            if (count > 0) {
                throw new RuntimeException(
                        String.format(
                                "操作编号%s为预置的基本操作编号，请修改%s[ID:%s]的自定义动作编号",
                                action.getCode(), viewModel.getCode(), viewModel.getID()));
            }

            SummaryInfo operation = new SummaryInfo();
            operation.setId(MessageFormat.format("{0}&^^&{1}", action.getID(), action.getCode()));
            operation.setCode(action.getCode());
            operation.setName(action.getName());

            operations.add(operation);
        }
        return operations;
    }
}
